﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;
using PPAP.Model;

namespace PPAP.Web.Part
{
    public partial class SupplierPPARWeb : PageBase
    {
        public string RID { get; set; }
        public string his { get; set; }
        PPARBO bo = new PPARBO();
        LogBO lbo = new LogBO();
        T_FileBO manager = new T_FileBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["RID"] != null)
                {
                    RID = Request.QueryString["RID"].ToString();
                    DataSet ds = bo.GetListForPPARid(RID);
                    DataRow dr = null;
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        dr = ds.Tables[0].Rows[0];
                        lblColor.Text = dr["color"].ToString();
                        lblPartName.Text = dr["bdntpartname"].ToString();
                        lblPartNo.Text = dr["bdntpartno"].ToString();
                        lblPPARdate.Text = dr["ppardate"].ToString() == "" ? "" : DateTime.Parse(dr["ppardate"].ToString()).ToString("yyyy-MM-dd");
                        LblPPARNo.Text = dr["pparno"].ToString();
                        lblRevisionNo.Text = dr["bdntrevisionno"].ToString();
                        lblRevisionStatus.Text = dr["bdntrevisionstatus"].ToString();
                        lblStatus.Text = ShowAuditStatus(dr["status"].ToString(),dr["isinapp"].ToString());
                        lblSupplierCode.Text = dr["suppliercode"].ToString();
                        lblSupplierName.Text = dr["suppliername"].ToString();
                        lbContactPerson.Text = dr["firstname"].ToString()==""?"":dr["firstname"].ToString() + " " + dr["name"].ToString();
                        lblPersonInformation.Text = dr["email"].ToString() == "" ? "" : dr["email"].ToString() + "/" + dr["telephone"].ToString();
                        lblPlanedPPARdate.Text = DateTime.Parse(dr["planedppardate"].ToString()).ToString("yyyy-MM-dd");
                        lblQlevel.Text = dr["qlevel"].ToString();
                        if (Request.QueryString["his"] == null) //不是从histroy页面跳转过来,根据状态显示不同tab页。
                        {
                            switch (dr["status"].ToString())
                            {
                                case "0":
                                    body.Attributes.Add("onload", "easytabs('1', '1');");
                                    break;
                                default:
                                    body.Attributes.Add("onload", "easytabs('1', '2');");
                                    break;
                            }
                        }
                        else
                            body.Attributes.Add("onload", "easytabs('1', '1');");
                    }
                }
                if (Request.QueryString["his"] != null)
                {
                    his = Request.QueryString["his"].ToString();
                    //btnBack.Visible = false;
                }
                else
                    his = "0";
            }
            if (Request.QueryString["RID"] != null)
            {
                RID = Request.QueryString["RID"].ToString();
            }
        }

        public string ShowAuditStatus(string Status,string isinapp)
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    if (isinapp == "0")
                        result = "<span style='color:red'>Un-allocated</span>";
                    else
                        result = "<span style='color:blue'>Allocated</span>";
                    break;
                case "1":
                    result = "<span style='color:red'>Created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>Submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>Not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Circulating</span>";
                    break;
                case "6":
                    result = "<span style='color:blue'>Released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>Decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            //}
            return result;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["his"] != null)
                Response.Redirect("PPARHistory.aspx");
             Response.Redirect("PPAPTasks.aspx");
        }

        /// <summary>
        /// Submit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["RID"] != null)
            {
                PPAR p = new PPARBO().GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                if (p != null)
                {
                    if (p.STATUS == 0)
                        Page.RegisterStartupScript( "AlertRedirect","<script language='javascript'>alert(\"Please first save PPAP\");</script>" );
                        //Page.RegisterStartupScript("AlertMessage", "<script language='javascript'>alert('Please first save PPAP');return false;</script>");
                    else
                    {
                        DataSet ds = manager.GetFileList(p.FJID);
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            Page.RegisterStartupScript("AlertRedirect", "<script language='javascript'>alert(\"No upload attachment\");</script>");
                        }
                        else
                        {

                            p.STATUS = 2;
                            int num = new PPARBO().UpdateForSupplier(p);
                            if (Session["UserName"] != null)
                                lbo.WriteLog(Session["UserName"].ToString(), "Submit PPAR", Request.QueryString["RID"].ToString());
                            if (num > 0)
                            {
                                Response.Write("<script language=javascript>top.frames['menu'].location.reload();</script>");
                                Response.Write("<script>top.frames['mainFrame'].location='PPAPTasks.aspx'</script>");
                            }
                        }
                    }
                }
            }
        }
    }
}